<?php

class ContactModel extends CI_Model {


	public function getById($id = NULL) {

		if ($id != NULL) {
			$this -> db -> where('id', $id);
			$query = $this -> db -> get('blog_contact');
			return $query -> row_array();
		} 
	}

	public function getAll($per_page = NULL, $offset = NULL) {

		$query = $this -> db -> get('blog_contact', $per_page, $offset);
		return $query -> result_array();

	}

	public function search($term = "") {

		if ($term != NULL && strlen($term)) {

			$array = array('name' => $term);

			$this -> db -> like($array);

			$query = $this -> db -> get('blog_contact');

			return $query -> result_array();

		}
	}

	public function add($data) {

		/**
		 * clear not necessary data.
		 * */
	
		
		unset($data['submit']);
		
		/**
		 * Adding the date
		 * */

		$data['date_created'] = date('Y-m-d H:i:s');

		$this -> db -> insert('blog_contact', $data);
		if($id=$this -> db -> insert_id()){
			
			$this->email->from($data['email'], $data['name']);
	
			$list = array('contact@raul-ivan.com', 'me@raul-ivan.com', 'raul.ivan.82@hotmail.com','reversible_82@hotmail.com');
			
			$this->email->to($list);

			$this->email->subject('website | www.raul-ivan.com | contact');
			$this->email->message($data['message']);	

			$this->email->send();
			
			
		}
		
		return $id;
	}

	
}
